package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.os.Build;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.live.LiveSelectCarBean;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3LicenseNumberAdapter extends RecyclerView.Adapter<X3LicenseNumberAdapter.NumberHolder> {

    private Context mContext;
    private List<LiveSelectCarBean> mList;

    public X3LicenseNumberAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
    }

    public void setData(List<LiveSelectCarBean> mList){
        this.mList.clear();
        this.mList = mList;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public NumberHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new NumberHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_item_license_number,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull NumberHolder holder, int position) {
            if(mList.get(position) != null){
                Glide.with(mContext).load(X3StringUtils.getImageUrl(mList.get(position).carBrandImgUrl)).apply(
                        new RequestOptions().placeholder(R.mipmap.x3_car_icon_no).skipMemoryCache(false).dontAnimate()
                ).into(holder.iv_img);
                if(!X3StringUtils.isEmpty(mList.get(position).num)){
                    String num = mList.get(position).num.replaceAll("<em>", "<font color='#0077FF'>").replaceAll("</em>", "</font>");

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        holder.tv_number.setText(Html.fromHtml(num,0));
                    }else {
                        holder.tv_number.setText(Html.fromHtml(num));
                    }

                }
            }

            if(mList.size() % 2 == 0){
                if(position + 1 == mList.size() || position + 2 ==mList.size()){
                    holder.view.setVisibility(View.GONE);
                }else {
                    holder.view.setVisibility(View.VISIBLE);
                }
            }else {
                if(position + 1 == mList.size()){
                    holder.view.setVisibility(View.GONE);
                }else {
                    holder.view.setVisibility(View.VISIBLE);
                }
            }


            holder.itemView.setOnClickListener(v -> {
                if(itemOnclick != null){
                    itemOnclick.onItemClick(mList.get(position).num.replaceAll("<em>", "").replaceAll("</em>", ""),mList.get(position).carBrandImgUrl);
                }
            });
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    class NumberHolder extends RecyclerView.ViewHolder {
        ImageView iv_img;
        TextView tv_number;
        LinearLayout ll_container;
        View view;
        public NumberHolder(@NonNull View itemView) {
            super(itemView);
            iv_img = itemView.findViewById(R.id.iv_img);
            tv_number = itemView.findViewById(R.id.tv_number);
            ll_container = itemView.findViewById(R.id.ll_container);
            view = itemView.findViewById(R.id.view);
        }
    }
  public   interface ItemOnclick{
    void onItemClick(String carNum,String imgUrl);
  }
  ItemOnclick itemOnclick;

  public void setOnitemClick(ItemOnclick itemOnclick){
      this.itemOnclick = itemOnclick;
  }
}
